package com.mnnu.controller;

import com.mnnu.pojo.Result;
import jakarta.servlet.http.Cookie;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@Slf4j
public class SessionController {

    @RequestMapping("/c1")
    public Result cookie1(HttpServletResponse httpServletResponse) {
        httpServletResponse.addCookie(new Cookie("login_code", "1"));
        return Result.success();
    }

    @RequestMapping("/c2")
    public Result cookie2(HttpServletRequest httpServletRequest) {
        Cookie[] cookies = httpServletRequest.getCookies();
        for (Cookie cookie : cookies) {
            String name = cookie.getName();
            if ("login_code".equals(name)) {
                return Result.success();
            }
        }
        return Result.error("cookie不存在，登录校验失败");
    }
}
